package com.whynot.frame.basic.mybatis.conroller;

import com.whynot.frame.basic.common.model.APIBase;
import com.whynot.frame.basic.mybatis.conroller.model.TreeNodes;
import com.whynot.frame.basic.mybatis.model.BaseModel;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.whynot.frame.basic.mybatis.service.BaseServiceImpl;
import com.whynot.frame.basic.mybatis.service.BaseTreeServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.Map;


/**
 * 描述: 基础的Ztree接口:
 * 作者: chenyao
 * 日期: 2018-01-30.
 * 项目名称: curtain-whynot-basic-mybatis
 * 版本: 1.0.0
 * JDK: since 1.8
 */
public abstract class BaseTreeController<M extends BaseTreeServiceImpl,T extends BaseModel> extends BaseTableController<M,T>{

    /**
     * 查询树形数据(同步)
     * @param param
     * @return
     */
    @ApiOperation(value = "通用的同步树查询方法", notes = "通用的同步树查询方法", response = APIBase.class)
    @RequestMapping(value = "/showTreeSync", method = RequestMethod.GET)
    public APIBase showTreeSync(@RequestParam Map<String, String> param){
        APIBase result=new APIBase();
        try{
            Collection<T> collection = this.service.getRoot(param);
            Collection<TreeNodes> data = this.service.findTreeNodes(param,collection,true,getTextPropertyName());
            result.setData(data);
        }catch(Exception e){
            result.setSuccess(false);
            result.setMsg("查询同步树出错!");
        }
        return result;
    }


    /**
     * 查询树形数据(异步)
     * @param param
     * @return
     */
    @ApiOperation(value = "通用的异步树查询方法", notes = "通用的异步树查询方法", response = APIBase.class)
    @RequestMapping(value = "/showTreeAsync", method = RequestMethod.GET)
    public APIBase showTreeAsync(@RequestParam Map<String, String> param){
        APIBase result=new APIBase();
        try{
            Collection<T> collection = this.service.getRoot(param);
            Collection<TreeNodes> data = this.service.findTreeNodes(param,collection,false,getTextPropertyName());
            result.setData(data);
        }catch(Exception e){
            result.setSuccess(false);
            result.setMsg("查询异步树出错!");
        }
        return result;
    }

    /**
     * 功能说明:取得用于显示节点文本的属性的名称
     */
    protected abstract String getTextPropertyName();

}